<script setup lang="ts">
import { ref } from 'vue'
// 导入校验规则
import { mobileRules, passwordRules, codeRules } from '@/utils/rules'
import { useUserStore } from '@/stores'
// 路由钩子函数（hooks function）
import { useRoute, useRouter } from 'vue-router'
import { showSuccessToast, showFailToast } from 'vant'
import { loginByPassword, loginByMobile } from '@/api/user'
// 导入发送验证码hooks函数
import { useSendCode } from '@/hooks'
// 表单数据
const formData = ref({
  mobile: '13230000036',
  password: 'abc12345'
})
// 使用hooks函数发送验证码
const { code, time, form, send } = useSendCode(formData.value)

// 是否同意网站协议
const agree = ref(false)
// 眼睛图标切换
const showPassword = ref(false)

// 切换手机号密码或者手机号短信登录
const isPass = ref(true)
// 表单提交
// 说明：表单校验通过执行
const store = useUserStore()
// 路由实例
const router = useRouter()
// 路由信息对象
// route === this.$route(vue2)
const route = useRoute()
const login = async () => {
  // 判断是否同意网站协议
  if (!agree.value) return showFailToast('请同意用户协议！')
  // 验证完毕，进行登录
  // const res = await loginByPassword(formData.value)
  const res = isPass.value
    ? await loginByPassword(formData.value)
    : await loginByMobile(formData.value.mobile, code.value)
  console.log('登录成功：', res.data)
  store.setUser(res.data)
  // 如果有回跳地址就进行回跳，没有跳转到个人中心
  // router.push(vue3) === this.$router.push(vue2)
  router.push((route.query.returnUrl as string) || '/user')
  showSuccessToast('登录成功')
}
</script>

<template>
  <!-- 1. 导航栏 -->
  <cp-nav-bar right-text="注册" @click-right="$router.push('/register')"></cp-nav-bar>
  <div class="login">
    <!-- 登录方式 -- 121>
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '短信验证码登录' }}</h3>
      <a href="javascript:;" @click="isPass = !isPass">
        <span>{{ !isPass ? '密码登录' : '短信验证码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- == form 表单 == -->
    <van-form ref="form" autocomplete="off" @submit="login">
      <!-- 1. 手机号 -->
      <van-field
        name="mobile"
        :rules="mobileRules"
        v-model="formData.mobile"
        placeholder="请输入手机号"
        type="tel"
      ></van-field>
      <!-- 2. 密码 -->
      <van-field
        v-model="formData.password"
        placeholder="请输入密码"
        :rules="passwordRules"
        :type="showPassword ? 'text' : 'password'"
        v-if="isPass"
      >
        <!-- 具名插槽 -->
        <template #button>
          <cp-icon
            @click="showPassword = !showPassword"
            :name="`login-eye-${showPassword ? 'on' : 'off'}`"
          ></cp-icon>
          <!-- <cp-icon name="login-eye-off"></cp-icon> -->
        </template>
      </van-field>
      <!-- 短信登录使用 -->
      <van-field v-else v-model="code" :rules="codeRules" placeholder="短信验证码">
        <template #button>
          <span :class="{ active: time > 0 }" @click="send">
            {{ time > 0 ? `${time}s后再次发送` : '发送验证码' }}
          </span>
        </template>
      </van-field>
      <!-- 3. 是否同意网站协议 -->
      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <!-- 4. 提交按钮 -->
      <div class="cp-cell">
        <!-- native-type="submit" 点击按钮触发表单提交 -->
        <van-button native-type="submit" block round type="primary">登 录</van-button>
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
  </div>
</template>

<style lang="scss" scoped>
.login {
  padding-top: 46px;
  &-head {
    padding: 30px 30px 50px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  // 表单样式
  .van-form {
    padding: 0 14px;
    .cp-cell {
      height: 52px;
      padding: 14px 16px;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      .van-checkbox {
        a {
          color: var(--cp-primary);
          padding: 0 5px;
        }
      }
    }
  }
}
</style>
